package com.android.aircondition.service;

import android.content.Context;
import android.content.SharedPreferences;

import com.android.aircondition.R;
import com.android.aircondition.constant.AppConstant;

public class GreeService {
	
	private final String GREE_PREFERENCE_KEY = "GREE_PREFERENCE_KEY";
	
	private Context context;
	
	public GreeService(Context context) {
		this.context = context;
	}
	
	public Mode getModePreference(){
		
		Mode mode = new Mode();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		boolean auto     = preference.getBoolean(Mode.MODE_AUTO, false);
		boolean zhileng  = preference.getBoolean(Mode.MODE_ZHILENG, false);
		boolean songfeng = preference.getBoolean(Mode.MODE_SONGFENG, false);
		boolean chushi   = preference.getBoolean(Mode.MODE_CHUSHI, false);
		boolean zhire    = preference.getBoolean(Mode.MODE_ZHIRE, false); 
		
		if(!(auto||zhileng||songfeng||chushi||zhire)){
			mode.setAuto(true);
			setModePreference(mode);
		}
		
		if(auto)     mode.setAuto(auto);
		if(zhileng)  mode.setZhileng(zhileng);
		if(songfeng) mode.setSongfeng(songfeng);
		if(chushi)   mode.setChushi(chushi);
		if(zhire)    mode.setZhire(zhire);
		
		return mode;
	}
	
	public void setModePreference(Mode mode){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Mode.MODE_AUTO, mode.getAuto());
		editor.putBoolean(Mode.MODE_ZHILENG, mode.getZhileng());
		editor.putBoolean(Mode.MODE_SONGFENG, mode.getSongfeng());
		editor.putBoolean(Mode.MODE_CHUSHI, mode.getChushi());
		editor.putBoolean(Mode.MODE_ZHIRE, mode.getZhire());
		
		editor.commit();
		
	}
	
	public Auxiliary getAuxiliaryPreference(){
		
		Auxiliary au = new Auxiliary();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		boolean jinghua   = preference.getBoolean(Auxiliary.AUXILIAY_JINGHUA, false);
		boolean ganzao    = preference.getBoolean(Auxiliary.AUXILIAY_GANZAO, false);
		boolean dengguang = preference.getBoolean(Auxiliary.AUXILIAY_DENGGUANG, false);
		boolean kuaileng  = preference.getBoolean(Auxiliary.AUXILIAY_KUAILENG, false);
		
		au.setJinghua(jinghua);
		au.setGanzao(ganzao);
		au.setDengguang(dengguang);
		au.setKuaileng(kuaileng);
		
		return au;
		
	}
	
	public void setAuxiliaryPreference(Auxiliary au){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Auxiliary.AUXILIAY_JINGHUA, au.getJinghua());
		editor.putBoolean(Auxiliary.AUXILIAY_GANZAO, au.getGanzao());
		editor.putBoolean(Auxiliary.AUXILIAY_DENGGUANG, au.getDengguang());
		editor.putBoolean(Auxiliary.AUXILIAY_KUAILENG, au.getKuaileng());
		
		editor.commit();
		
	}
	
	public Update getUpdatePreference(){
		
		Update clear = new Update();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		boolean xiqi         = preference.getBoolean(Update.UPDATE_XIQI, false);
		boolean paiqi        = preference.getBoolean(Update.UPDATE_PAIQI, false);
		boolean zhinengxiqi  = preference.getBoolean(Update.UPDATE_ZHINENG_XIQI, false);
		boolean zhinengpaiqi = preference.getBoolean(Update.UPDATE_ZHINENG_PAIQI, false);
		boolean guanbi       = preference.getBoolean(Update.UPDATE_GUANBI, false);
		
		clear.setXiqi(xiqi);
		clear.setPaiqi(paiqi);
		clear.setZhinengxiqi(zhinengxiqi);
		clear.setZhinengpaiqi(zhinengpaiqi);
		clear.setGuanbi(guanbi);
		
		return clear;
		
	}
	
	public void setUpdatePreference(Update clear){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Update.UPDATE_XIQI, clear.getXiqi());
		editor.putBoolean(Update.UPDATE_PAIQI, clear.getPaiqi());
		editor.putBoolean(Update.UPDATE_ZHINENG_XIQI, clear.getZhinengxiqi());
		editor.putBoolean(Update.UPDATE_ZHINENG_PAIQI, clear.getZhinengpaiqi());
		editor.putBoolean(Update.UPDATE_GUANBI, clear.getGuanbi());
		
		editor.commit();
		
	}
	
	public Silence getSilencePreference(){
		
		Silence silence = new Silence();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		boolean auto   = preference.getBoolean(Silence.SILENCE_AUTO, false);
		boolean kaiqi  = preference.getBoolean(Silence.SILENCE_KAIQI, false);
		boolean guanbi = preference.getBoolean(Silence.SILENCE_GUANBI, false);
		
		silence.setAuto(auto);
		silence.setGuanbi(guanbi);
		silence.setKaiqi(kaiqi);
		
		return silence;
		
	}
	
	public void setSilencePreference(Silence silence){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Silence.SILENCE_AUTO, silence.getAuto());
		editor.putBoolean(Silence.SILENCE_KAIQI, silence.getKaiqi());
		editor.putBoolean(Silence.SILENCE_GUANBI, silence.getGuanbi());
		
		editor.commit();
			
	}
	
	public Temperature getTemperaturePreference(){
		
		Temperature tempearture = new Temperature();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		float unit_c   = preference.getFloat(Temperature.UNIT_C, 16);
		float unit_f  = preference.getFloat(Temperature.UNIT_F, 61);
		boolean unit_key = preference.getBoolean(Temperature.C_UNIT, true);
		
		tempearture.setUnit_C(unit_c);
		tempearture.setUnit_F(unit_f);
		tempearture.setcUnit(unit_key);
		
		return tempearture;
		
	}
	
	public void setTemperaturePreference(Temperature temperature){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Temperature.C_UNIT, temperature.iscUnitKey());
		editor.putFloat(Temperature.UNIT_C, temperature.getUnit_C());
		editor.putFloat(Temperature.UNIT_F, temperature.getUnit_F());
		
		editor.commit();
			
	}
	
	public Setting getSettingPreference(){
		
		Setting setting = new Setting();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		boolean switchOn = preference.getBoolean(Setting.SETTING_SWITCHON, true);
		String  ip       = preference.getString(Setting.SETTING_IP, context.getText(R.string.network_default_ip).toString());
		String  port     = preference.getString(Setting.SETTING_PORT, context.getText(R.string.network_default_port).toString());
		String  title    = preference.getString(Setting.SETTING_TITLE, context.getText(R.string.network_my_airconditioner).toString());
		
		setting.setSwitchOn(switchOn);
		setting.setIp(ip);
		setting.setPort(port);
		setting.setTitle(title);
		
		return setting;
	}
	
	public void setSettingPreference(Setting setting){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Setting.SETTING_SWITCHON, setting.isSwitchOn());
		editor.putString(Setting.SETTING_IP, setting.getIp());
		editor.putString(Setting.SETTING_PORT, setting.getPort());
		editor.putString(Setting.SETTING_TITLE, setting.getTitle());
		
		editor.commit();
		
	}
	
	public void setWindyPreference(Windy windy){
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putBoolean(Windy.WINDY_ISAUTO, windy.isAuto());
		editor.putInt(Windy.WINDY_SPEED, windy.getSpeed());
		
		editor.commit();
	}
	
	public Windy getWindyPreference(){
		
		Windy windy = new Windy();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		boolean isAuto = preference.getBoolean(Windy.WINDY_ISAUTO, false);
		int     speed  = preference.getInt(Windy.WINDY_SPEED, 0);
		
		windy.setAuto(isAuto);
		windy.setSpeed(speed);
		
		return windy;
		
	}
	
	public void setTimerPreference(Timer timer){
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		
		editor.putInt(Timer.TIMER_POWERON_HOUR,     timer.getPowerOnHour());
		editor.putInt(Timer.TIMER_POWERON_MIN,      timer.getPowerOnMin());
		editor.putInt(Timer.TIMER_POWERON_SETTING,  timer.getPowerOnSetting());
		editor.putBoolean(Timer.TIMER_POWERON,      timer.isPowerOn());
		editor.putInt(Timer.TIMER_POWEROFF_HOUR,    timer.getPowerOffHour());
		editor.putInt(Timer.TIMER_POWEROFF_MIN,     timer.getPowerOffMin());
		editor.putInt(Timer.TIMER_POWEROFF_SETTING, timer.getPowerOffSetting());
		editor.putBoolean(Timer.TIMER_POWEROFF,     timer.isPowerOff());
		
		editor.commit();
		
	}
	
	public Timer getTimerPreference(){
		
		Timer timer = new Timer();
		
		SharedPreferences preference =  this.context.getSharedPreferences(GREE_PREFERENCE_KEY,Context.MODE_PRIVATE);
		
	   int powerOnHour     = preference.getInt(Timer.TIMER_POWERON_HOUR, AppConstant.NO_0);
	   int powerOnMin      = preference.getInt(Timer.TIMER_POWERON_MIN, AppConstant.NO_0);
	   int powerOnSetting  = preference.getInt(Timer.TIMER_POWERON_SETTING, Timer.TIMES_ONLY_ONE);
	   boolean powerOn     = preference.getBoolean(Timer.TIMER_POWERON, false);
       int powerOffHour    = preference.getInt(Timer.TIMER_POWEROFF_HOUR, AppConstant.NO_0);
	   int powerOffMin     = preference.getInt(Timer.TIMER_POWEROFF_MIN, AppConstant.NO_0);
	   int powerOffSetting = preference.getInt(Timer.TIMER_POWEROFF_SETTING, Timer.TIMES_ONLY_ONE);
	   boolean powerOff    = preference.getBoolean(Timer.TIMER_POWEROFF, false);
	   
	   timer.setPowerOnHour(powerOnHour);
	   timer.setPowerOnMin(powerOnMin);
	   timer.setPowerOnSetting(powerOnSetting);
	   timer.setPowerOn(powerOn);
	   timer.setPowerOffHour(powerOffHour);
	   timer.setPowerOffMin(powerOffMin);
	   timer.setPowerOffSetting(powerOffSetting);
	   timer.setPowerOff(powerOff);
	   
		
	   return timer;
	}
	
}
